package com.timi_licai.timi_licai_account.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.timi_licai.timi_licai_account.AddCategoryActivity;
import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.model.ShowCategory;

import java.util.List;

/**
 * Created by LGH on 2015/3/16.
 */
public class AddCategoryCustomAdapter extends BaseAdapter {
    private Context context;
    private List<ShowCategory> categoryList;

    public AddCategoryCustomAdapter(Context context, List<ShowCategory> categoryList) {
        this.context = context;
        this.categoryList = categoryList;
    }


    @Override
    public int getCount() {
        return categoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_add_category, parent, false);
            viewHolder.imageIcon = (ImageView) convertView.findViewById(R.id.item_add_category_image);
            viewHolder.txtName = (TextView) convertView.findViewById(R.id.item_add_category_name);
            viewHolder.txtEdit = (TextView) convertView.findViewById(R.id.item_add_category_editName);
            viewHolder.imageDelete = (ImageView) convertView.findViewById(R.id.item_add_category_choice);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.imageIcon.setImageResource(R.drawable.type_big_1);
        viewHolder.txtName.setText(categoryList.get(position).getTypeName());
        viewHolder.txtEdit.setText("编辑");
        viewHolder.imageDelete.setImageResource(R.drawable.delete);
        viewHolder.imageDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (context instanceof AddCategoryActivity) {
                    AddCategoryActivity activity = (AddCategoryActivity) context;
                    activity.deleteItem(position);
                }
            }
        });
        viewHolder.txtEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddCategoryActivity activity = (AddCategoryActivity) context;
                activity.updateItem(position);
            }
        });
        return convertView;
    }


    private class ViewHolder {
        public ImageView imageIcon;
        public TextView txtName;
        public TextView txtEdit;
        public ImageView imageDelete;
    }
}
